#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>

#define LENGTH 1024

int fun(int *x, int *y){
	long a;
	printf("I am fun\n");
	//scanf("%4ld",&a);
	//printf("the num is:%ld\n",a);
	*x = 10;
	*y = 11;
	return 999;
}


long fibonacci(long n) {
	if(n == 0 || n == 1) {
		return n;
	} else {
		return fibonacci(n - 1) + fibonacci(n - 2);
	}
}

/* 
* 求最大值 
* 返回值是int类型，返回两个整数中较大的一个 
*/  
int max(int a, int b) {  
	return a > b ? a : b;  
}  
	     
/* 
* 求最小值 
* 返回值是int类型，返回两个整数中较小的一个 
*/  
int min(int a, int b) {  
	return a < b ? a : b;  
} 

/* 
 * 指针函数的定义 
 * 返回值是指针类型int * 
 */  
int *f1(int a, int b) {  
    int *p = (int *)malloc(sizeof(int));  
    printf("The memeory address of p = 0x%x \n", p);  
    memset(p, 0, sizeof(int));  
    *p = a + b;  
    printf("*p = %d \n", *p);  
  
    return p;  
}


int openFile() {
	int f;
	char *path = "/home/Learn/ls.txt";
	mode_t f_attrib = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
	f = open(path, O_RDWR);
	if(f == -1) {
		f = open(path, O_RDWR | O_CREAT, f_attrib);
		if(f != -1) {
			puts("创建新文件");
		} else {
			puts("创建失败");
			return 1;
		}
	} else {
		puts("打开成功");
	}
	close(f);
	return 0;
}

int readFile() {
	char *path = "/home/Learn/ls.txt";
	char c[LENGTH];
	int f, i, j = 0;
	
	f = open(path, O_RDWR, LENGTH);//每次读入缓冲区的字数为LENGTH
	if(f != -1) {
		i = read(f, c, LENGTH);
		
		if(i > 0) {
			for(; i>0; i--){
				putchar(c[j++]);
			}
		} else {
			perror("读取");
		}
	} else {
			perror("打开文件");
	}
	return 0;
}

int writeFile() {
	char c[LENGTH];
	int f, i;
	char *path = "/home/Learn/ls.txt";
	
	puts("输入要保存的信息:\n");
	if((i = read(0, c, LENGTH)) < 1) {
		perror("read读取失败");
		return 1;
	}
	
	f = open(path, O_RDWR, 0664);
	if(f != -1) {
		if(write(f, c, i) != i) {
			perror("write");
		}
		puts("writeFile保存文件成功");
		close(f);
	} else {
		perror("writeFile打开文件");
	}
	return 0;
}

void file_io() {
	FILE *fp;
	char *path = "/home/Learn/ls.txt";
	fp = fopen(path, "r");
	
	if(fp != NULL) {
		puts("file_io打开文件成功");
	} else {
		perror("file_io打开文件");
		return;
	} 
	
	if(fclose(fp) != -1) {
		puts("file_io关闭文件成功");
	} else {
		perror("file_io关闭文件");
		return;
	}
}